STRMATCH

Tip: See also the IDL_String::Contains and IDL_String::Matches methods, which provide similar functionality but with an object-oriented interface.

The STRMATCH function compares its search string, which can contain wildcard characters, against the input string expression. The result is an array with the same structure as the input string expression. Those elements that match the corresponding input string are set to True (1), and those that do not match are set to False (0).

The wildcards understood by STRMATCH are similar to those used by the standard UNIX shell:

Wildcard Character

Description

*

Matches any string, including empty strings.

?

Matches any single character.

[...]

Matches any one of the enclosed characters. A pair of characters separated by “-” matches any character lexically between the pair, inclusive. If the first character following the opening [ is a !, any character not enclosed is matched.

To prevent one of these characters from acting as a wildcard, precede it with a backslash character (e.g. "\*" matches the asterisk character). Quoting any other (non-wildcard) character (including \ itself) is equivalent to the character (e.g. "\a" is the same as "a").

Examples

Find all 4-letter words in a string array that begin with “f” or “F” and end with “t” or “T”:

str = ['foot', 'Feet', 'fate', 'FAST', 'ferret', 'fort']

PRINT, str[WHERE(STRMATCH(str, 'f??t', /FOLD_CASE) EQ 1)]

This results in:

foot Feet FAST fort

Find words of any length that begin with “f” and end with “t”:

str = ['foot', 'Feet', 'fate', 'FAST', 'ferret', 'fort']

PRINT, str[WHERE(STRMATCH(str, 'f*t', /FOLD_CASE) EQ 1)]

This results in:

foot Feet FAST ferret fort

Syntax

Result = STRMATCH( String, SearchString , /FOLD_CASE )

Return Value

Returns 1 if the pattern specified by SearchString exists in String, or 0 otherwise. If the String argument contains an array of strings, the result is an array of 1s and 0s with the same number of elements as String, indicating which elements contain SearchString.

Arguments

String

A scalar string or string array to be searched.

SearchString

A scalar string containing the pattern to search for in String. The pattern string can contain wildcard characters as discussed above.

Keywords

FOLD_CASE

The comparison is usually case sensitive. Setting the FOLD_CASE keyword causes a case insensitive match to be done instead.

Additional Examples

Find 4-letter words beginning with “f” and ending with “t”, with any combination of “o” and “e” in between:

str = ['foot', 'Feet', 'fate', 'FAST', 'ferret', 'fort']
PRINT, str[WHERE(STRMATCH(str, 'f[eo][eo]t', /FOLD_CASE) EQ 1)]

This results in:

foot Feet

Find all words beginning with “f” and ending with “t” whose second character is not the letter “o”:

str = ['foot', 'Feet', 'fate', 'FAST', 'ferret', 'fort']
PRINT, str[WHERE(STRMATCH(str, 'f[!o]*t', /FOLD_CASE) EQ 1)]

This results in:

Feet FAST ferret

Version History

5.3

Introduced

See Also

String Operations, String Processing, STRCMP, STRJOIN, STREGEX, STRMID, STRPOS, STRSPLIT, IDL_String